﻿//设置车位购物车数据
var SetCarShopCart = function (data) {
    var inter = setInterval(function () {
            if (carloadend) {
                for (var i in data) {
                    if (data.hasOwnProperty(i)) {
                        var cart = data[i];
                        window.shopcarcount++;
                        $("#tip").html("还有" + (3 - window.shopcarcount) + "次选购机会");
                        carShopList.push({
                            shopid: parseInt(cart.D_ID),
                            id: cart.D_FieldID,
                            state: cart.D_State,
                            time: cart.D_Time,
                            oper: cart.D_Operation
                        });
                    }
                }
                if (carData !== null) {
                    showCarSeat();
                    clearInterval(inter);
                }
            }
        },
        200);
}

//车位购物车列表
function showCarSeat() {
    for (var i in carShopList) {
        if (carShopList.hasOwnProperty(i)) {
            var idd = carShopList[i].id;
            var shopid = carShopList[i].shopid;
            var shopstate = carShopList[i].state;
            var da = carData[idd];
            if ($("#carsetbutton table tr[tid=" + shopid + "]").length === 0) {
                var tr = CreatTr(i, shopstatearr[shopstate]);
                var time = carShopList[i].time;
                tr.attr("tid", shopid);
                $("#carsetbutton table").append(tr);

                var div = $("<div  id=\"down" +
                    i +
                    "\" class=\"timedown\"><div id=\"cartCountdown" +
                    i +
                    "\" class=\"time\"></div></div>");
                $("#downdiv").append(div);
                //倒计时
                window.getCountDown({
                    obj: $('#cartCountdown' + i),
                    timestamp: time,
                    format: "%M:%S"
                });
                showInGrid(da, parseInt(i), shopstate);
            }
        }
    }
}

//购物车过期
function carttimeout(index, state) {
    $("#cartCountdown" + index).html("车位" + shopstatearr[state] + "！");
}

//更新购物车
function UpdateCarState(id, state) {
    $("#carsetbutton").hide();
    closelayer();
    for (var i in carShopList) {
        if (carShopList.hasOwnProperty(i)) {
            var data = carShopList[i];
            if (data.id === id) {
                window.spaceId = id;
                data.state = state;
                //清空倒计时
                $("#cartCountdown" + i).countdown("stop");
                carttimeout(i, data.state);
                break;
            }
        }
    }
    showCarSeat();
    closeOrderWin(true);
}

var shopstatearr = ["", "确认预定", "取消预定", "提交成功", "已过期"];
function showInGrid(data, index) {
    var monitor = data.Monitor;
    if (monitor === 0) {
        $("#carseat" + index).html(data.CarNo);
        $("#carinfo" + index).html(data.CarInfo);
        $("#cartotl" + index).html(data.CarTotal);
        $("#carbenefitd" + index).html(data.CarBenefit);
        $("#carremarkd" + index).html(data.Remark);
        $("#cardefensed" + index).html(data.Defense);
        $("#cararead" + index).html(data.Area);
    }
}

function CreatTr(index, state) {
    var tr = $("<tr></tr>");
    tr.append("<td><div id=\"carseat" + index + "\"></div></td>");
    tr.append("<td><div id=\"carinfo" + index + "\"></div></td>");
    tr.append("<td><div id=\"cartotl" + index + "\"></div></td>");
    tr.append("<td><div id=\"carbenefitd" + index + "\"></div></td>");
    tr.append("<td><div id=\"carremarkd" + index + "\"></div></td>");
    tr.append("<td><div id=\"cardefensed" + index + "\"></div></td>");
    tr.append("<td><div id=\"cararead" + index + "\"></div></td>");
    tr.append("<td><div>" + state + "</div></td>");
    return tr;
}

//倒计时
function showTimeCountDown(index) {
    var arr = [0, 1, 2];
    for (var i in arr) {
        if (arr.hasOwnProperty(i)) {
            if (i === index) {
                $("#down" + i).show();
            } else {
                $("#down" + i).hide();
            }
        }
    }
}

//获取购物车下标
function getIndexByRoomCart(id) {
    var index = -1;
    for (var i in carShopList) {
        if (carShopList.hasOwnProperty(i)) {
            var cart = carShopList[i];
            if (cart.id.toString() === id.toString()) {
                index = i;
                break;
            }

        }
    }
    return index;
}

//获取购物车编号
function GetCarCartId(carid) {
    var id = "";
    for (var i in carShopList) {
        if (carShopList.hasOwnProperty(i)) {
            var cart = carShopList[i];
            if (cart.id.toString() === carid.toString() && cart.state === 1) {
                id = cart.shopid;
            }

        }
    }
    return id.toString();
}